# == Schema Information
#
# Table name: parises
#
#  id          :bigint(8)        not null, primary key
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  user_id     :bigint(8)
#  micopost_id :bigint(8)
#

class Parise < ApplicationRecord
  belongs_to :user
  belongs_to :micopost
  validates :user_id, :micopost_id, absence: false
  # has_one :micopost
  # has_one :user
  def parise_post()
    if Parise.where(user: self.user).where(micopost: self.micopost).count == 0
      self.save
    else
      errors.add :user, "已点赞"
      errors.add :micopost, "已点赞"
      false
    end
  end

  def delete_parise
    # todo 更新like num
    self ? self.destroy : false
  end

end
